﻿using HungryCrowdLibrary;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Web;
using System.Web.UI.WebControls;

namespace HungryCrowd.HelperFunctions
{
    public static class Helpers
    {
        public static string Salt { get; set; }

        public static bool IsNewUser(string username)
        {
            var isNewUser = false;
            var user = API.UserManager.VerifyPassword(x => x.Username == username);            
            // create user.
            return isNewUser = (user == null || user.Id <= 0);
        }

        public static byte[] GenerateSaltedPassword(string password, string salt)
        {

            if (string.IsNullOrWhiteSpace(salt))
            {
                var generateSalt = new byte[64];
                RNGCryptoServiceProvider.Create().GetBytes(generateSalt);
                // Create hash password.
                var strSalt = Convert.ToBase64String(generateSalt);
                Salt = strSalt;
            }
            else
            {
                Salt = salt;
            }
         
            var hashPwd = new byte[] { };
            using (var sha256 = new SHA256Managed())
            {
                var bytes = Encoding.Default.GetBytes(string.Format("{0}{1}", Salt, password));
                hashPwd = sha256.ComputeHash(bytes);
            }
            return hashPwd;
        }

        public static void PopulateDropDownLists(this DropDownList dropDownList, Type type)
        {
            switch (type.Name)
            {
                case "Location":
                    {
                        dropDownList.DataSource = API.LocationManager.GetList(x=> x.IsActive == true);
                        dropDownList.DataTextField = "Name";
                        dropDownList.DataValueField = "Id"; 
                        dropDownList.DataBind();
                        break;
                    }
                case "Market":
                    {
                        dropDownList.DataSource = API.MarketManager.GetList(x=> x.IsActive == true);
                        dropDownList.DataTextField = "Name";
                        dropDownList.DataValueField = "Id";
                        dropDownList.DataBind();
                        break;
                    }
                case "Trader":
                    {
                        dropDownList.DataSource = API.TraderManager.GetList(x=> x.IsActive == true);
                        dropDownList.DataTextField = "Name";
                        dropDownList.DataValueField = "Id";
                        dropDownList.DataBind();
                        break;
                    }
                case "Category":
                    {
                        dropDownList.DataSource = API.CategoryManager.GetList(x => x.IsActive == true);
                        dropDownList.DataTextField = "Name";
                        dropDownList.DataValueField = "Id";
                        dropDownList.DataBind();
                        break;
                    }
            }

            dropDownList.Items.Insert(0, new ListItem { Text = "Please select.." });
            
        }

    }
}